import { defineFakeRoute } from "vite-plugin-fake-server/client";
import { faker } from "@faker-js/faker/locale/zh_CN";
import { getAsyncMenus } from "../async-menus";

let hasInit = false;

const data: any[] = [
  {
    id: 1,
    roleName: "系统管理员",
    code: "admin",
    permissions: [
      "System",
      "Testing",
      "SystemUser",
      "SystemRole",
      "SystemMenu",
      "SystemDept",
      "TestingComponent",
      "backend:btn:add",
      "backend:btn:edit",
      "backend:btn:delete",
      "TestingPermission",
      "TestingAdmin",
      "TestingAbout"
    ]
  },
  {
    id: 2,
    roleName: "普通用户",
    code: "common",
    permissions: [
      "Testing",
      "TestingComponent",
      "backend:btn:add",
      "backend:btn:edit",
      "TestingPermission",
      "TestingCommon",
      "TestingAbout"
    ]
  }
];

export const getData = () => {
  if (!hasInit) {
    hasInit = true;
    for (const index in data) {
      data[index] = {
        ...data[index],
        remark: faker.book.genre(),
        sort: "99",
        status: Math.random() > 0.5 ? true : false,
        createTime: faker.date.between({ from: "2000-01-01", to: Date.now() }),
        updateTime: faker.date.between({ from: "2000-01-01", to: Date.now() })
      };
    }
  }
  return data.sort((a, b) => a.sort - b.sort);
};

export default defineFakeRoute([
  {
    url: "/system/roles/menus",
    method: "get",
    //statusCode: 404,
    response: () => {
      return {
        success: true,
        message: "操作成功",
        data: getAsyncMenus()
      };
    }
  },
  {
    url: "/system/roles/page",
    method: "post",
    //statusCode: 404,
    response: ({ body }) => {
      return {
        success: true,
        message: "操作成功",
        data: {
          list: getData().slice(
            (body.page - 1) * body.pageSize,
            body.page * body.pageSize
          ),
          total: getData().length,
          pageSize: body.pageSize,
          currentPage: body.page
        }
      };
    }
  },
  {
    url: "/system/roles",
    method: "post",
    response: ({ body }) => {
      return {
        success: true,
        message: "操作成功",
        data: {
          ...body,
          id: 999
        }
      };
    }
  },
  {
    url: "/system/roles/:id",
    method: "get",
    response: ({ params }) => {
      return {
        success: true,
        message: "操作成功",
        data: getData().find(item => item.id == params.id)
      };
    }
  },
  {
    url: "/system/roles/:id",
    method: "put",
    response: ({ params }) => {
      return {
        success: true,
        message: "操作成功",
        data: getData().find(item => item.id == params.id)
      };
    }
  },
  {
    url: "/system/roles/:id/permissions",
    method: "put",
    response: () => {
      return {
        success: true,
        message: "操作成功"
      };
    }
  },
  {
    url: "/system/roles/:id/status",
    method: "put",
    response: () => {
      return {
        success: true,
        message: "操作成功"
      };
    }
  },
  {
    url: "/system/roles/:id",
    method: "delete",
    statusCode: 404,
    response: () => {
      return {
        success: true,
        message: "操作成功"
      };
    }
  },
  {
    url: "/system/roles",
    method: "delete",
    response: () => {
      return {
        success: false,
        message: "禁止删除"
      };
    }
  }
]);
